class GistsController < ApplicationController

    before_filter :login_required, :except => [:index, :show, :filter]

    layout "default"

    # GET /Gists
    # GET /Gists.xml
    def index
        @gists = Gist.find(:all)
        respond_to do |format|
            format.html # index.html.erb
            format.xml  { render :xml => @gists }
        end
    end

    def filter
        @tag = params[:tag] || ''
        @gists = Gist.get_tagged(@tag, {:limit => 100})
    end
    
    # GET /Gists/1
    # GET /Gists/1.xml
    def show
        @gist = Gist.find(params[:id])

        respond_to do |format|
            format.html # show.html.erb
            format.xml  { render :xml => @gist }
        end
    end

    # GET /Gists/new
    # GET /Gists/new.xml
    def new
        @gist = Gist.new
        @gist.title = params["t"] unless params["t"].nil?
        @gist.result = params["c"] unless params["c"].nil?
        
        @citation = Citation.new
        @citation.url = params["u"] unless params["u"].nil?
                
        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => [@gist, @citation] }
        end
    end

    # GET /Gists/1/edit
    def edit
        @gist = Gist.find(params[:id])
        @citation = @gist.citation
    end

    # POST /Gists
    # POST /Gists.xml
    def create

        @citation = Citation.find_or_initialize_by_url(params[:citation][:url]) # citations are unique by url
        @gist = Gist.new(params[:gist])
        @gist.user = current_user
        
        respond_to do |format|
            begin
                @citation.save! # save the citation first or there won't be an id for the Gist
                @gist.citation = @citation # now you can assign the citation
                @gist.save!                
                flash[:notice] = 'Gist was successfully created.'
                format.html { redirect_to(@gist) }
                format.xml  { render :xml => @gist, :status => :created, :location => @gist }
            rescue ActiveRecord::RecordInvalid => e
                @gist.valid? # make sure the Gist model validates
                format.html { render :action => "new" }
                format.xml  { render :xml => [@gist.errors, @citation.errors] }
            end
        end
    end

    # PUT /Gists/1
    # PUT /Gists/1.xml
    def update
        @gist = Gist.find(params[:id])

        respond_to do |format|
            if @gist.update_attributes(params[:gist])
                flash[:notice] = 'Gist was successfully updated.'
                format.html { redirect_to(@gist) }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @gist.errors }
            end
        end
    end

    # DELETE /Gists/1
    # DELETE /Gists/1.xml
    def destroy
        @gist = Gist.find(params[:id])
        @gist.destroy

        respond_to do |format|
            format.html { redirect_to(gists_url) }
            format.xml  { head :ok }
        end
    end
    
end
